#include "fig_player_craft.h"


FIGPlayerCraft::ShipSpriteInfo::ShipSpriteInfo() : 
										m_ShipIndex(0),
										m_PositionCounter(0)
{
	
}

void	FIGPlayerCraft::ShipSpriteInfo::Reset(const math::Vec2& Pos)
{
	m_ShipIndex = (int)FIGPlayerCraft::CRAFT_MIDDLE;
	m_PositionCounter = 0;
	m_PrevSwayPos =	Pos;
}


void	FIGPlayerCraft::ShipSpriteInfo::Tick(float dt , 
														   const math::Vec2& Pos)
{
	// LEFT , RIGHT , MIDDLE determination
	if(m_PositionCounter <= 0)
	{
		const float MIN_DIFF_LEFT = -0.005f;
		const float MIN_DIFF_RIGHT = 0.005f;
        
		float DiffX = Pos.x - m_PrevSwayPos.x;
		if( DiffX < MIN_DIFF_LEFT)
		{
			// goinig left
			GoingLeft();
		}
		else if ( DiffX > MIN_DIFF_RIGHT)
		{
			// going right
			GoingRight();
		}
		else
		{
			// going mid
			GoingMid();
		}
        
		m_PositionCounter = POSITION_COUNTER_DURATION;
		m_PrevSwayPos = Pos;
    }
	else
	{
		m_PositionCounter -= dt;
	}
}

void	FIGPlayerCraft::ShipSpriteInfo::GoingLeft()
{
	if( m_ShipIndex <= 0)
		return;
    
	m_ShipIndex--;
}

void	FIGPlayerCraft::ShipSpriteInfo::GoingRight()
{
	if( m_ShipIndex >= CRAFT_SPRITE_COUNT-1)
		return;
    
	m_ShipIndex++;
}

void	FIGPlayerCraft::ShipSpriteInfo::GoingMid()
{
	if(m_ShipIndex < CRAFT_MIDDLE)
		m_ShipIndex++;
	else if(m_ShipIndex > CRAFT_MIDDLE)
		m_ShipIndex--;
    
	//m_ShipIndex = CRAFT_MIDDLE;
}
